var $table = $("#orderTab");

$(function () {
	//时间控件
	$.initDateScope('startDate','endDate',false);
    initTable();
    //条件查询
    $("#queryOrderBtn").click(function () {
        $table.bootstrapTable('refresh');	 //使用post提交
    });
});

function initTable() {
    $table.bootstrapTable({
        dataType: "json",
         showColumns: true, 
        pagination: true, //分页
        striped: true,
        cache: false,	//是否使用缓存，默认是true
        pageSize: 10,
        pageNumber: 1,
        queryParamsType: 'limit',
        smartDisplay: true,
        sidePagination: "server",
        method: "post",	//使用post提交
        queryParams: queryParams,
        url: '../order/getCompleteOrderInfoByPage.htm',
        contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
        columns: [
            {
                title: "序号",
                align: 'center',
                formatter:function(value,row,index){
                    return index + 1;
                }
            },
            {
                title: "预约单编号",
                field: "orderNum",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "是否回单",
                field: "orderRestate",
                align: 'center',
                valign: 'middle',
                formatter: function(value, row, index){
                    var orderRestate = "-";
                    if(row.orderRestate == 0){
                    	orderRestate = '<span style="color:green;">回单正常</span>';
                    }else if(row.orderRestate == 1){
                    	orderRestate ='<span style="color:red;">回单异常</span>';
                    }
                    return orderRestate;
                }
            },
            {
                title: "状态",
                field: "orderState",
                align: 'center',
                valign: 'middle',
                formatter: function(value, row, index){
                    var orderState = "-";
                    if(row.orderState == 0){
                    	orderState = '预约状态';
                    }else if(row.orderState == 1){
                    	orderState ='业务员接单';
                    }else if(row.orderState == 2){
                    	orderState ='上门确认';
                    }
                    else if(row.orderState == 3){
                    	orderState ='司机接单';
                    }
                    else if(row.orderState == 4){
                    	orderState ='司机接货';
                    }
                    else if(row.orderState == 5){
                    	orderState ='司机送达';
                    }
                    else if(row.orderState == 6){
                    	orderState ='人工确认';
                    }
                    else if(row.orderState == -1){
                    	orderState ='异常单';
                    }
                    return orderState;
                }
            },
            {
                title: "订单生成时间",
                field: "orderInsDate",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "支付状态",
                field: "orderPayState",
                align: 'center',
                valign: 'middle',
                formatter: function(value, row, index){
                    var orderPayState = "-";
                    if(row.orderPayState == 0){
                    	orderPayState = '<span style="color:green;">已支付</span>';
                    }else if(row.orderState == 1){
                    	orderPayState ='<span style="color:red;">未支付</span>';
                    }
                    return orderPayState;
                }
            },
            {
                title: "是否搬运",
                field: "orderToCarry",
                align: 'center',
                valign: 'middle',
                formatter: function(value, row, index){
                    var orderToCarry = "-";
                    if(row.orderToCarry == 0){
                    	orderToCarry = '<span style="color:green;">搬运</span>';
                    }else if(row.orderToCarry == 1){
                    	orderToCarry ='<span style="color:red;">无需搬运</span>';
                    }
                    return orderToCarry;
                }
            },
            {
                title: "是否打包",
                field: "orderToPack",
                align: 'center',
                valign: 'middle',
                formatter: function(value, row, index){
                    var orderToCarry = "-";
                    if(row.orderToCarry == 0){
                    	orderToCarry = '<span style="color:green;">打包</span>';
                    }else if(row.orderToCarry == 1){
                    	orderToCarry ='<span style="color:red;">无需打包</span>';
                    }
                    return orderToCarry;
                }
            },
            {
                title: "楼层数",
                field: "orderFloorNum",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "集装箱数",
                field: "orderBoxNum",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "是否是搬运单",
                field: "orderIsMove",
                align: 'center',
                valign: 'middle',
                formatter: function(value, row, index){
                    var orderIsMove = "-";
                    if(row.orderIsMove == 0){
                    	orderIsMove = '搬运单';
                    }else if(row.orderIsMove == 1){
                    	orderIsMove ='同城配送单';
                    }
                    return orderIsMove;
                }
            },
            {
                title: "支付时间",
                field: "orderPayTime",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "支付方式",
                field: "orderPayWay",
                align: 'center',
                valign: 'middle',
                formatter: function(value, row, index){
                    var orderPayWay = "-";
                    if(row.orderPayWay == 0){
                    	orderPayWay = '微信';
                    }else if(row.orderPayWay == 1){
                    	orderPayWay ='支付宝';
                    }
                    else if(row.orderPayWay == 3){
                    	orderPayWay ='银行卡';
                    }
                    return orderPayWay;
                }
            },
            {
                title: "全部费用",
                field: "orderAllPay",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "预约时间",
                field: "orderPrearrangeTime",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "订单运费",
                field: "orderCarriage",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "订单距离",
                field: "orderDistance",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "打包费用",
                field: "orderPackPay",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "搬家费用",
                field: "orderMovePay",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "发件人姓名",
                field: "ownerName",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "收件人姓名",
                field: "rOwnerName",
                align: 'center',
                valign: 'middle'
            },
            {
            	 title: "司机",
                 field: "driverRealName",
                 align: 'center',
                 valign: 'middle'
            },
            {
                title: "货物名",
                field: "cargoName",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "微信昵称",
                field: "viewNickName",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "公司名",
                field: "comnumName",
                align: 'center',
                valign: 'middle'
            }
        ],
        onClickRow: function(row, $element, field){	//当点击某一行时，该行背景颜色改变
        	var trObj = $($element[0]);
        	var dataIndex = trObj.attr('data-index');
        	$("tr[data-index != '" + dataIndex + "']").removeAttr('style');
        	trObj.attr('style', 'background: #B4EEB4');
        },
        showExport: true,  //是否显示导出按钮
        buttonsAlign:"right",  //按钮位置
        exportDataType: "basic",              //basic', 'all', 'selected'
        exportTypes:['excel'],  //导出文件类型
        exportOptions:{
            fileName: '数据导出',  //文件名称设置
            worksheetName: 'sheet1',  //表格工作区名称
            tableName: '总台帐报表',
            excelstyles: ['background-color', 'color', 'font-size', 'font-weight'],
        },
        onLoadSuccess: function (data) {  //加载成功时执行
		    var sum_1 = 0;	//总订单数
		    var sum_2 = 0;	//总金额
		    var sum_3 = 0;	//搬家总订单数
		    var sum_4 = 0;	//搬家总金额
		    var sum_5 = 0;	//短运总订单数
		    var sum_6 = 0;	//短运总金额
		    for (var o in data.rows) {
		        sum_2 = parseFloat(sum_2) + parseFloat(data.rows[o].orderAllPay);
				if(data.rows[o].orderIsMove == 0){
					sum_3 = parseInt(sum_3) + 1;
					sum_4 = parseFloat(sum_4) + parseFloat(data.rows[o].orderAllPay);
				}else if(data.rows[o].orderIsMove == 1){
					sum_5 = parseInt(sum_5) + 1;
					sum_6 = parseFloat(sum_6) + parseFloat(data.rows[o].orderAllPay);
				}
		    }
		    sum_1 = data.rows.length;
		    var rows = [];
		    rows.push({
		        orderNum: '总订单数：' + sum_1 + ' 条',
		        orderInsDate: '总金额：' + number_format(sum_2,2,'.',',') + ' 元',
		        orderFloorNum: '搬家总订单数：' + sum_3 + ' 条',
		        orderBoxNum: '搬家总金额：' + number_format(sum_4,2,'.',',') + ' 元',
		        orderAllPay: '短运总订单数：' + sum_5 + ' 条',
		        orderPrearrangeTime: '短运总金额：' + number_format(sum_6,2,'.',',') + ' 元'
		    });
		    $table.bootstrapTable('append', rows);
		}
    });
}


//分页参数，条件查询时的参数
function queryParams(queryParamsType){
	var temp = {
		limit: queryParamsType.limit,   //页面大小
		offset: queryParamsType.offset,  //页码
		orderNum: $.trim($('#orderNum').val()),
		orderState: $.trim($('#orderState').val()),
		orderPayState: $.trim($('#orderPayState').val()),
		ownerName: $.trim($('#ownerName').val()),
		rOwnerName: $.trim($('#rOwnerName').val()),
		cargoName: $.trim($('#cargoName').val()),
		orderPayWay: $.trim($('#orderPayWay').val()),
		orderIsMove: $.trim($('#orderIsMove').val()),
		endDate: $.trim($('#endDate').val()),
		startDate: $.trim($('#startDate').val())
	};
	return temp;
}

/*
* 参数说明：
* number：要格式化的数字
* decimals：保留几位小数
* dec_point：小数点符号
* thousands_sep：千分位符号
* */
function number_format(number, decimals, dec_point, thousands_sep) {
    number = (number + '').replace(/[^0-9+-Ee.]/g, '');
    var n = !isFinite(+number) ? 0 : +number,

        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.floor(n * k) / k;
        };
    s = (prec ? toFixedFix(n, prec) : '' + Math.floor(n)).split('.');
    var re = /(-?\d+)(\d{3})/;
    while (re.test(s[0])) {
        s[0] = s[0].replace(re, "$1" + sep + "$2");
    }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }
    return s.join(dec);
}
